package com.hwadee.job.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hwadee.job.entity.Company;
import com.hwadee.job.entity.Recruit;
import com.hwadee.job.entity.User;
import com.hwadee.job.service.CompanyService;
import com.hwadee.job.service.RecruitService;
import com.hwadee.job.util.PageInfo;
import com.hwadee.job.util.Pager;

@RequestMapping("/recruit")
@Controller
public class RecruitController {

	@Autowired
	private CompanyService companyService;
	@Autowired
	private RecruitService recruitService;
	
	@PostMapping("/crecruit")
	public String suRecruit(Recruit recruit,Model model,HttpSession session) {
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(date);
		
		User user=(User) session.getAttribute("user");
		String name=user.getuName();
		
		recruit.setReName(name);
		recruit.setReDate(str);
		
		recruitService.insert(recruit);
		return "redirect:/recruit/crecruit1";
		
	}
	
	@GetMapping("/crecruit1")
	public String suRecruit1(@RequestParam(name = "pageIndex", required = true, defaultValue = "1") Integer pageIndex,
			@RequestParam(name = "size", required = true, defaultValue = "5") Integer size,Model model,HttpSession session) {
		User user=(User) session.getAttribute("user");
		
		PageInfo<Recruit> pageUtil = recruitService.findPage1(pageIndex, size);
		
		
		Company com=companyService.selectByName(user.getuName());
		model.addAttribute("company", com);
		model.addAttribute("recruit", pageUtil);
		return "company/company";
	}
	

	@GetMapping("/crecruit2")
	public String suRecruit2(@RequestParam(name = "pageIndex", required = true, defaultValue = "1") Integer pageIndex,
			@RequestParam(name = "size", required = true, defaultValue = "5") Integer size, Model model,HttpSession session) {
		User user=(User) session.getAttribute("user");
		PageInfo<Recruit> pageUtil = recruitService.findPage(pageIndex, size, user.getuName());
		
		
//	List<Recruit> list=recruitService.selectAll();
//	List<Recruit> list=recruitService.findByName(user.getuName());
//	model.addAttribute("recruit", list);
	
		model.addAttribute("recruit", pageUtil);
		
		return "company/intro2";
	}
	
	@GetMapping("/crecruit3")
	public String suRecruit3(Model model,HttpSession session) {
		return "company/intro";
		
	}
	
	@GetMapping("/delete")
	public String reDelete(int reId, Model model) {
		recruitService.deleteByPrimaryKey(reId);
		return "redirect:/recruit/crecruit2";
		
	}
	
	@GetMapping("/select")
	public String reSelect(int reId, Model model) {
		Recruit recruit=recruitService.selectByPrimaryKey(reId);
		model.addAttribute("update", recruit);
		return "company/intro3";
	}
	@PostMapping("/update")
	public String reUpdate(Recruit recuit, Model model) {
		recruitService.updateByPrimaryKey(recuit);
		return "redirect:/recruit/crecruit2";
		
	}
	
	@GetMapping("/info")
	public String reSelect1(int reId, Model model) {
		Recruit recruit=recruitService.selectByPrimaryKey(reId);
		model.addAttribute("update", recruit);
		return "company/intro4";
	}
	
	/**
	 * 关键字查询
	 */
	@PostMapping("/search")
	public String search(String rename,String reCompanyName,String rePositiontype,String reDate,
			String reWorkingplace,String reMoney, Model model) {
		List<Recruit> re=recruitService.selectKeyWord(rename, reCompanyName, rePositiontype, reDate, reWorkingplace, reMoney);
		model.addAttribute("search", re);
		return "company/search";
	}
	/**
	 * 关键字查询
	 */
//	@RequestMapping("/search1")
//	public String search1(@RequestParam(name = "pageIndex", required = true, defaultValue = "1") Integer pageIndex,
//			@RequestParam(name = "size", required = true, defaultValue = "2") Integer size,Recruit re, Model model,HttpSession session) {
//		
//		
//		PageInfo<Recruit> pageUtil = recruitService.selectKeyWord1(pageIndex, size,re.getRePositiontype(),re.getReEdurequire(),re.getReWorkingplace(), re.getReMoney());
//		
//
//		model.addAttribute("recruit", pageUtil);
//		
//		return "student/search";
//	}

	@RequestMapping("/search1")
	public String getAllUser(Model model, Recruit re, Pager pager,HttpServletRequest request) {
		if (pager == null) {
			pager = new Pager();
		}
		pager.setTotal(recruitService.reCount(re.getRePositiontype(),re.getReEdurequire(),re.getReWorkingplace() ,re.getReMoney()));
		List<Recruit> findAll = recruitService.selectKeyWord1(pager.getStartRecord(), pager.getPageSize(),re.getRePositiontype(),re.getReEdurequire(),re.getReWorkingplace() ,re.getReMoney());
		request.setAttribute("search", findAll);
		request.setAttribute("pager", pager);
		
		return "student/search";
	}

	
}
